package com.yang.zngcxyxkjs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.zngcxyxkjs.mapper.ApplicationMapper;
import com.yang.zngcxyxkjs.pojo.Application;
import com.yang.zngcxyxkjs.pojo.Student;
import com.yang.zngcxyxkjs.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ApplicationController {

    @Autowired
    private ApplicationMapper applicationMapper;


    @RequestMapping("/studentApply")
    public String toStuApply(Integer comId,String comName,Model model, HttpSession session){

        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);
        model.addAttribute("comId",comId);
        model.addAttribute("comName",comName);

        return "student/studentApply.html";
    }

    @RequestMapping("/stuApplyCompetition")
    public String studentApplication(Application application,Model model, HttpSession session){

        System.out.println("------------------");
        System.out.println(application);
        System.out.println("------------------");
        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);
        model.addAttribute("comName",application.getComName());
//        model.addAttribute("stuNo",application.getStuNo());
//        model.addAttribute("stuName",application.getStuName());
        boolean b = applicationMapper.insertApplication(application);
        if(b){
            return "student/applySuccess.html";
        }else{
            return "error.html";
        }

    }

//    @RequestMapping("/toApplyRecords")
//    public String toApplyRecords(Model model, HttpSession session){
//        Student stu = (Student)session.getAttribute("stu");
//        model.addAttribute("stu",stu);
//        return "student/studentApplyRecords.html";
//    }

    @RequestMapping("/selectAllApplicationsByStuNo")
    public String selectAllApplicationsByStuNo(@RequestParam(defaultValue = "1") Integer currentPageNum,String stuNo,Model model, HttpSession session){
        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);
        PageHelper.startPage(currentPageNum,5);
        applicationMapper.updateApplicationResult();
        applicationMapper.defaultApplicationComment();
        List<Application> list = applicationMapper.selectAllApplicationsByStuNo(stuNo);
        PageInfo<Application> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "student/studentApplyRecords.html";

    }

    @RequestMapping("/selectAllApplicationsByTeaName")
    public String selectAllApplicationsByTeaName(@RequestParam(defaultValue = "1") Integer currentPageNum,String teaName,Model model, HttpSession session){
        Teacher tea = (Teacher)session.getAttribute("tea");
        model.addAttribute("tea",tea);
        model.addAttribute("teaName",tea.getTeaName());
        int teaPageSize = 2;
        PageHelper.startPage(currentPageNum,teaPageSize);
//        applicationMapper.updateApplicationResult();
//        applicationMapper.defaultApplicationComment();
        List<Application> list = applicationMapper.selectAllApplicationsByTeaName(teaName);
        List<Application> listPass = applicationMapper.selectAllApplicationsByTeaNamePass(teaName);
        PageInfo<Application> pageInfo = new PageInfo<>(list);
        PageInfo<Application> pageInfoPass = new PageInfo<>(listPass);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("pageInfoPass",pageInfoPass);
        return "teacher/teacherApplyRecords.html";

    }

    @RequestMapping("/cancelApplication")
    public String cancelApplication(Integer comId,String stuNo,Model model,HttpSession session){
        boolean c = applicationMapper.cancelApplication(comId,stuNo);
        Student stu = (Student)session.getAttribute("stu");
        return "redirect:/selectAllApplicationsByStuNo?stuNo=" + stu.getStuNo();
    }

    @RequestMapping("/teacherAuditPassById")
    public String teacherAuditPassById(Integer id,Model model, HttpSession session){
        Teacher tea = (Teacher)session.getAttribute("tea");
        String teaName = tea.getTeaName();
        model.addAttribute("tea",tea);
        boolean tapb = applicationMapper.teacherAuditPassById(id);
        return "redirect:/selectAllApplicationsByTeaName?teaName=" + teaName;
    }

    @RequestMapping("/teacherGetApplicationInfoById")
    public String teacherGetApplicationInfoById(Integer id,Model model){
        Application application = applicationMapper.getApplicationInfoById(id);
        System.out.println("vvvvvvvvvvvvvvvvvvvvvvv");
        System.out.println(application);
        System.out.println("vvvvvvvvvvvvvvvvvvvvvvv");
        model.addAttribute("application",application);
        return "teacher/teacherGetApplicationInfoById.html";
    }

    @RequestMapping("/unPassComment")
    public String unPassCommentBycomIdAndStuNo(String comment,Integer comId,String stuNo,HttpSession session,Model model){
        Teacher tea = (Teacher)session.getAttribute("tea");
        model.addAttribute("tea",tea);
        boolean upc = applicationMapper.unPassComment(comment,comId,stuNo);
        applicationMapper.updateApplicationResultNotPassByComIdAndStuNo(comId, stuNo);
        return "redirect:/selectAllApplicationsByTeaName?teaName=" + tea.getTeaName();
    }

}
